package com.yunshipei.ui.fragment;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.RepetitionStatusModel;
import com.yunshipei.ui.activity.AlarmSetActivity;
import com.yunshipei.utils.DrawableUtils;

import io.rong.eventbus.EventBus;

//import de.greenrobot.event.EventBus;

public class RepetitionFragment extends BaseFragment implements CheckBox.OnCheckedChangeListener {
    private boolean statusAarry[] = new boolean[9];
    private CheckBox checkBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    private CheckBox checkBox4;
    private CheckBox checkBox5;
    private CheckBox checkBox6;
    private CheckBox checkBox7;
    private CheckBox checkEveryDay;
    private CheckBox notDay;
    private View view;
    private RepetitionStatusModel repetitionStatusModel;

    public RepetitionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_repetition, container, false);
        initView();
        return view;
    }

    public void initView() {
        checkBox1 = (CheckBox) view.findViewById(R.id.repe_checkbox1);
        checkBox2 = (CheckBox) view.findViewById(R.id.repe_checkBox2);
        checkBox3 = (CheckBox) view.findViewById(R.id.repe_checkBox3);
        checkBox4 = (CheckBox) view.findViewById(R.id.repe_checkBox4);
        checkBox5 = (CheckBox) view.findViewById(R.id.repe_checkBox5);
        checkBox6 = (CheckBox) view.findViewById(R.id.repe_checkBox6);
        checkBox7 = (CheckBox) view.findViewById(R.id.repe_checkBox7);
        checkEveryDay = (CheckBox) view.findViewById(R.id.repe_checkBox8);
        notDay = (CheckBox) view.findViewById(R.id.repe_checkBox9);
        checkBox1.setButtonDrawable(getCheckBoxDraw());
        checkBox2.setButtonDrawable(getCheckBoxDraw());
        checkBox3.setButtonDrawable(getCheckBoxDraw());
        checkBox4.setButtonDrawable(getCheckBoxDraw());
        checkBox5.setButtonDrawable(getCheckBoxDraw());
        checkBox6.setButtonDrawable(getCheckBoxDraw());
        checkBox7.setButtonDrawable(getCheckBoxDraw());
        checkEveryDay.setButtonDrawable(getCheckBoxDraw());
        notDay.setButtonDrawable(getCheckBoxDraw());
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        checkBox4.setOnCheckedChangeListener(this);
        checkBox5.setOnCheckedChangeListener(this);
        checkBox6.setOnCheckedChangeListener(this);
        checkBox7.setOnCheckedChangeListener(this);
        checkEveryDay.setOnCheckedChangeListener(this);
        notDay.setOnCheckedChangeListener(this);
        repetitionStatusModel = new RepetitionStatusModel();
    }

    private Drawable getCheckBoxDraw() {
        int color = EnterConfig.getInstance().getGlobalColor();
        Drawable normalDraw = getActivity().getResources().getDrawable(R.drawable.select_off);
        Drawable single = getActivity().getResources().getDrawable(R.drawable.icon_checked_src);
        return DrawableUtils.getYspCheckBoxDraw(color, normalDraw, single);
    }


    @Override
    void handlerSendMessage() {
        Message message = new Message();
        message.what = AlarmSetActivity.REPETITIONTIME;
        Bundle bundle = new Bundle();
        bundle.putBooleanArray("status", getStatusAarry());
        message.setData(bundle);
        handler.sendMessage(message);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.repe_checkbox1:
                statusAarry[0] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox2:
                statusAarry[1] = isChecked;
                notDay.setChecked(false);
                statusAarry[8] = false;
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox3:
                statusAarry[2] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox4:
                statusAarry[3] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox5:
                statusAarry[4] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox6:
                statusAarry[5] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox7:
                statusAarry[6] = isChecked;
                notDay.setChecked(false);
                setCheckBox8(false);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            case R.id.repe_checkBox8:
                statusAarry[7] = isChecked;
                if (isChecked) {
                    checkBox1.setChecked(false);
                    checkBox2.setChecked(false);
                    checkBox3.setChecked(false);
                    checkBox4.setChecked(false);
                    checkBox5.setChecked(false);
                    checkBox6.setChecked(false);
                    checkBox7.setChecked(false);
                    notDay.setChecked(false);
                    for (int j = 0; j < 7; j++) {
                        statusAarry[j] = false;
                    }
                    statusAarry[8] = false;
                }
                checkEveryDay.setChecked(isChecked);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                Log.e("repetitionFragment", isChecked + "");
                break;
            case R.id.repe_checkBox9:
                statusAarry[8] = isChecked;
                if (isChecked) {
                    checkBox1.setChecked(false);
                    checkBox2.setChecked(false);
                    checkBox3.setChecked(false);
                    checkBox4.setChecked(false);
                    checkBox5.setChecked(false);
                    checkBox6.setChecked(false);
                    checkBox7.setChecked(false);
                    checkEveryDay.setChecked(false);
                    for (int j = 0; j < 7; j++) {
                        statusAarry[j] = false;
                    }
                    statusAarry[7] = false;
                }
                notDay.setChecked(isChecked);
                repetitionStatusModel.setRepetitionStatusArray(statusAarry);
                EventBus.getDefault().post(repetitionStatusModel);
                break;
            default:
                Log.e("repetitionFragment---default", " default ");
                break;
        }
    }

    public boolean[] getStatusAarry() {
        return statusAarry;
    }

    private void setCheckBox8(boolean bool) {
        statusAarry[8] = bool;
        checkEveryDay.setChecked(bool);
    }
}